package m.store.action;

import m.appserver.ActionRedirect;
import m.base.action.BaseAction;
import m.base.util.LevelUtil;
import m.base.util.PowerUtil;
import m.common.action.ActionMeta;
import m.common.action.RequestMeta;
import m.fun.action.CountInfoAction;
import m.fun.action.ProduceInfoAction;
import m.html.VueListResult;
import m.html.element.Element;
import m.html.element.attr.Align;
import m.html.element.attr.ButtonType;
import m.html.element.attr.TagColor;
import m.html.form.FormButton;
import m.html.list.*;
import m.store.model.OrderRepositoryCheck;
import m.store.model.StoreNumber;
import m.store.service.OrderRepositoryCheckService;
import m.store.util.ColorUtil;
import m.system.exception.MException;
import m.system.util.JSONMessage;
import m.system.util.StringUtil;

import java.sql.SQLException;

@ActionMeta(name = "store/orderModel")
public class OrderModelAction extends BaseAction {
    @Override
    public void presetVueListResult(VueListResult result) {
        try {
            result.setFunction(LevelUtil.exportFunc(),false);
        } catch (Exception ignored) {}
        result.setFocusRefresh(true);
        result.setHeight(-1);
    }

    @RequestMeta(params = {"oid","orderNo"})
    public ActionRedirect toViewOrder(String oid,String orderNo) throws MException {
        String first=orderNo.substring(0,1);
        JSONMessage param=new JSONMessage().push("model",new JSONMessage().push("oid",oid));
        switch (first){
            case StoreNumber.Type_Stock:
                return new ActionRedirect(StockInfoAction.class,"toView",param);
            case StoreNumber.Type_Sale:
                return new ActionRedirect(SaleInfoAction.class,"toView",param);
            case StoreNumber.Type_Pay:
                return new ActionRedirect(PayAmountAction.class,"toView",param);
            case StoreNumber.Type_Income:
                return new ActionRedirect(IncomeAmountAction.class,"toView",param);
            case StoreNumber.Type_Produce:
                return new ActionRedirect(ProduceInfoAction.class,"toView",param);
            case StoreNumber.Type_Count:
                return new ActionRedirect(CountInfoAction.class,"toView",param);
        }
        throw new MException(this.getClass(),"参数错误");
    }
    protected ListColumn getStatusColumn(){
        return getStatusColumn("status");
    }
    protected ListColumn getStatusColumn(String field){
        String dict="m.store.model.SaleInfo!status";
        return toElementColumn("状态", field, 100,
            ListElement.dot(dict, ColorUtil.getOrderStatus())
        ).setAlign(Align.CENTER).setFilter(true).setDictType(dict);
    }
    protected void fillOrderColumn(VueListResult result){
        result.addCol(toTextColumn("登记人", "inputUser.realname", 100, true).setAlign(Align.CENTER));
        result.addCol(toDateTimeColumn("录入时间", "inputDate", 130, true).setAlign(Align.CENTER));
        if(this instanceof StockInfoAction||this instanceof SaleInfoAction||this instanceof CountInfoAction||this instanceof ProduceInfoAction){
            result.addCol(toLinkColumn("仓库审核","repositoryStatus",100,true,
                new ListEvent(actionUrl(OrderRepositoryCheckAction.class,"viewList"), EventType.MODAL,1300)
                    .addParam("params[orderOid]","oid")
            ).setDictOption(OrderRepositoryCheck.class,"status").setAlign(Align.CENTER));
        }
        result.addCol(toTextColumn("审核人", "checkUser.realname", 100, true).setAlign(Align.CENTER));
        result.addCol(toDateTimeColumn("审核时间", "checkDate", 130, true).setAlign(Align.CENTER));
    }
    protected String getDefRepParam() throws Exception {
        String defRepOid= PowerUtil.getDefRepository().getOid();
        return "{oid:"+(null!=defRepOid?"'"+defRepOid+"'":"null")+"}";
    }

    /**
     * 获取表单审核按钮
     * 数据model，继承OrderModel
     */
    protected FormButton getCheckFormButton(String orderOid) throws MException, SQLException {
        String msg=getService(OrderRepositoryCheckService.class).verifyCheckOver(orderOid,false);
        return FormButton.succ("审核通过","doCheck").setConfirm("确定要审核通过吗？")
            .setTest("(!model.repositoryStatus)||model.repositoryStatus=='"+OrderRepositoryCheck.Status_Yes+"'")
            .setElse(
                Element.tag("a-tooltip").A("title",StringUtil.noSpace(msg)).E(
                    Element.tag("a-tag").E("仓库审核中").A("color", TagColor.Error.toString())
                )
            );
    }
}
